import { defineStore } from "pinia";
import { routes, asyncRoutes } from "@/router/index.js";
import { getUserDetailAPI } from "@/api/user";
import router from "@/router/index.js";
const useSettingStore = defineStore("settingStore", {
  state: () => {
    return {
      routeList: routes, // 路由列表
      //导航栏tabBar
      tabBarList: [],
      //按钮列表
      buttonList: [],
      user: "",
    };
  },
  actions: {
    //添加导航栏tabBar
    addTabBar(route) {
      const index = this.tabBarList.findIndex(
        (item) => item.fullPath === route.fullPath
      );
      if (index === -1 && route.title !== "首页") {
        this.tabBarList.push(route);
      }
    },
    //根据下标移除导航栏tabBar
    removeTabBar(index) {
      this.tabBarList.splice(index, 1);
    },
    //获取菜单列表和按钮列表
    async getUserInfo() {
      let result = await getUserDetailAPI();
      console.log(result);
      this.user = result.data;
      let menuArr = result.data?.menusArr.map((item) => item.menuValue);
      this.buttonList = result.data?.btnsArr.map((item) => item.ButtonValue);
      asyncRoutes[0].children = asyncRoutes[0].children.filter((item)=>{
        if(item.name=='welcome') {
          return true
        }
        return menuArr.includes(item.name)
      })
      this.routeList = asyncRoutes[0].children;
      router.addRoute(...asyncRoutes)
      router.addRoute({
        path: "/:pathMatch(.*)*",
        name: "notFound",
        component: () => import("@/views/404/index.vue"),
        meta: {
          isShow: false,
        },
      });
    },
  },
  getters: {},
});

export default useSettingStore;
